<template>
  <div>
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="store.formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="zjm" label="">
          <a-input v-model:value="store.formState.zjm" class="w-64" placeholder="姓名/助记码/病案号/住院号/床位号" />
        </a-form-item>
        <a-form-item name="enterDateRange" label="入院日期" class="w-[330px]">
          <a-range-picker v-model:value="store.formState.enterDateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item name="outDateRange" label="出院日期" class="w-[330px] ml-2">
          <a-range-picker v-model:value="store.formState.outDateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item name="dabz" label="归档备注" class="mx-2 w-[330px]">
          <a-input v-model:value="store.formState.dabz" placeholder="请输入" />
        </a-form-item>
        <a-form-item name="bazt" class="w-[330px]">
          <a-radio-group v-model:value="store.formState.bazt">
            <a-radio value="">全部</a-radio>
            <a-radio value="0">未提交</a-radio>
            <a-radio value="1">已提交</a-radio>
            <a-radio value="2">已归档</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item name="icd10Name" label="出院诊断" class="w-[330px]">
          <a-input v-model:value="store.formState.icd10Name" placeholder="请输入" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2">
      <div class="flex justify-end mb-2">
        <!-- <BaseExport type="药库" name="供应商维护" code="00001006" :search-params="formState">导出</BaseExport> -->
        <BaseExport
          type="病案"
          name="病案首页"
          code="000350"
          :search-params="{
            ...store.formState,
            rystdate: store.formState.enterDateRange?.[0],
            ryeddate: store.formState.enterDateRange?.[1],
            cystdate: store.formState.outDateRange?.[0],
            cyeddate: store.formState.outDateRange?.[1]
          }"
          >导出</BaseExport
        >
      </div>
      <BaseTable fill :data-source="tableData" :loading="loading" :scroll="{ x: 2400 }" column-code="000350" :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'baztStr'">
            <a-tag v-if="record.bazt === 0" color="blue">未提交</a-tag>
            <a-tag v-if="record.bazt === 1" color="green">已提交</a-tag>
            <a-tag v-if="record.bazt === 2" color="default">已归档</a-tag>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-space>
              <a-button type="link" size="small" @click="onClickEdit(record)">编辑</a-button>
              <a-button type="link" size="small" @click="onClickFile(record)">归档</a-button>
              <a-button type="link" size="small" @click="onPrint(record)">打印</a-button>
              <TableMoreButton>
                <a-button type="link" size="small" @click="onClickScore(record)">评分</a-button>
              </TableMoreButton>
            </a-space>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="open" :row-data="rowData" @success="getData" />
    <SelectPrint v-model:open="openPrint" @success="onSuccessPrint" />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import { post } from '@/utils/request'
// import useUserStore from '@/store/user'
import SelectPrint from '@/components/MedicalRecord/components/SelectPrint.vue'
import Edit from './components/Edit.vue'
import useStore from './store.js'

const store = useStore()
const router = useRouter()
// const { empName } = useUserStore()
const rowData = ref({})
const formRef = ref(null)
const open = ref(false)
const openPrint = ref(false)
const { loading, pagination, tableData, getData } = useTable({
  url: '/med/MedicalRecord/getMedicalRecordHomepageList',
  immediate: true,
  params: store.formState,
  formatParams: (params) => {
    return {
      ...params,
      rystdate: params.enterDateRange?.[0],
      ryeddate: params.enterDateRange?.[1],
      cystdate: params.outDateRange?.[0],
      cyeddate: params.outDateRange?.[1]
    }
  }
})

// 跳转基本信息
const onClickEdit = (row) => {
  router.push({
    path: '/medical-record/home-page/basic-info',
    query: {
      inplsh: row.inplsh
    }
  })
}

// 归档
const onClickFile = (row) => {
  open.value = true
  rowData.value = row
}

const onPrint = async (row) => {
  rowData.value = row
  openPrint.value = true
}

const onSuccessPrint = async (printArray = []) => {
  if (printArray.length > 0) {
    if (printArray.includes(1)) {
      const { code, msg, data } = await post('/med/MedicalRecord/printHomePageOfMedicalRecords', { inplsh: rowData.value.inplsh })
      if (code !== 0) return message.error(msg)
      window.openExe?.rpt(data) //调用winform打印
    }
    if (printArray.includes(2)) {
      const {
        code: codeBack,
        msg: msgBack,
        data: dataBack
      } = await post('/med/MedicalRecord/printHomePageOfMedicalRecordsBack', { inplsh: rowData.value.inplsh })
      if (codeBack !== 0) return message.error(msgBack)
      window.openExe?.rpt(dataBack) //调用winform打印
    }
    if (printArray.includes(3)) {
      if (getSystemConfig('NMI_REGION_CODE') === '02') {
        const { code, msg, data } = await post('/med/MedicalRecord/printHomePageOfMedicalRecordsFy', { inplsh: rowData.value.inplsh, jslb: 'SF' })
        if (code !== 0) return message.error(msg)
        window.openExe?.rpt(data) //调用winform打印
      }
    }
  }
}
</script>
